package com.adun;

import java.io.Console;
import java.util.concurrent.atomic.AtomicInteger;

class MyThread implements Runnable{
    public static volatile int  i=0;
    public static AtomicInteger a=new AtomicInteger();
    @Override
    public synchronized void run() {
        for (int j = 0; j <100 ; j++) {
            i++;
            add();
            System.out.println(Thread.currentThread().getName()+"+1:"+i);
        }
    }

    public void add(){
        a.getAndIncrement();
    }
    public  int getI() {
        System.out.println(i);
        return i;
    }

    public void setI(int i) {
        this.i = i;
    }
}

/**
 * @author adun 2-200
 * @date 2020/11/25 09:55
 */
public class ThreadNoSafeTest {
    public static void main(String[] args) {
        MyThread myThreadOne = new MyThread();
        MyThread myThreadTwo = new MyThread();

        new Thread(myThreadOne,"A").start();
        new Thread(myThreadTwo,"B").start();

//        try {
//            Thread.sleep(1);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        while (Thread.activeCount()>2){
            Thread.yield();
        }
        myThreadTwo.getI();
        System.out.println(myThreadTwo.a);
    }
}
